package wiiusej.apptest;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class WiimoteMain{

	//Logger	
	private static Logger fileLogger;
	
	//Inputs
	private static String[] inputParameters; 
	
	//Run modes
	private final static int NORMAL = 0; 
	private final static int NET = 1;
	private final static int RUN_MODES = 6;
	
	
	/*
	 *  Run modes&Input parameters:
	 *  0 - normal (run mode, run measurements, initial timeout)
	 *  1 - network measurement (run mode, measurement length, initial timeout)
	 *  2 - acceleration measurements (run mode, measurement length, initial timeout)
	 *  3 - packet measurements (run mode, measurement length, initial timeout)
	 *  4 - kmeans cluster algorithm (run mode, measurement length, initial timeout, padding)
	 *  5 - accuracy check: motion recognition comparison between heuristic and k-means algorithms
	 *  (run mode, measurement length)
	 */
	
		
	public static void main(String[] args) {

		//Log configuration
		PropertyConfigurator.configure("log4j.properties");
		
		fileLogger = Logger.getLogger("main");
		
		
		//we got the input parameters
		inputParameters = args;		
		
		getInputParameters(args);
			
	}		
		
	private static void getInputParameters(String[] args) {
		
		if(args.length > 0){
			
			int runMode = Integer.parseInt(args[0]);			
			fileLogger.info("RunMode: " + runMode + "  ArgumentEntered: " + args[0]);
						
			if(checkInputs(args, runMode)){
				initApp(runMode);
			}		
			
		}else{			
			fileLogger.error("NO VALID INPUT PARAMETERS");			
			fileLogger.info("EXECUTION FINISHED!!");			
			
		}		
		
	}
	private static boolean checkInputs(String[] args, int runMode){
		
		if(runMode > RUN_MODES-1 || runMode < 0){
			fileLogger.error("NO VALID RUN MODE");
			return false;
		}
		
		if(args[1] == null || Integer.parseInt(args[1]) < 1 
				|| args[2] == null || Integer.parseInt(args[2]) < 1){			
			//the simulation cannot continue	
			fileLogger.error("NO VALID INPUT PARAMETERS");
			return false;							
		}
		
		return true;	
		
		
	}

	private static void initApp(int runMode) {		
		Runnable initApp = new AppInitializator(runMode, inputParameters);
		initApp.run();		
	}

	
	
	
}

	
